﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using bookMaster.AssetSystem.Entities;
using bookMaster.AssetSystem.Data;

#endregion

namespace bookMaster.AssetSystem.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="UsuariosProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class UsuariosProviderBaseCore : EntityProviderBase<bookMaster.AssetSystem.Entities.Usuarios, bookMaster.AssetSystem.Entities.UsuariosKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByIdRecursoFromUsuariosRecursos
		
		/// <summary>
		///		Gets Usuarios objects from the datasource by IdRecurso in the
		///		UsuariosRecursos table. Table Usuarios is related to table Recursos
		///		through the (M:N) relationship defined in the UsuariosRecursos table.
		/// </summary>
		/// <param name="_idRecurso"></param>
		/// <returns>Returns a typed collection of Usuarios objects.</returns>
		public TList<Usuarios> GetByIdRecursoFromUsuariosRecursos(int _idRecurso)
		{
			int count = -1;
			return GetByIdRecursoFromUsuariosRecursos(null,_idRecurso, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets bookMaster.AssetSystem.Entities.Usuarios objects from the datasource by IdRecurso in the
		///		UsuariosRecursos table. Table Usuarios is related to table Recursos
		///		through the (M:N) relationship defined in the UsuariosRecursos table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_idRecurso"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Usuarios objects.</returns>
		public TList<Usuarios> GetByIdRecursoFromUsuariosRecursos(int _idRecurso, int start, int pageLength)
		{
			int count = -1;
			return GetByIdRecursoFromUsuariosRecursos(null, _idRecurso, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Usuarios objects from the datasource by IdRecurso in the
		///		UsuariosRecursos table. Table Usuarios is related to table Recursos
		///		through the (M:N) relationship defined in the UsuariosRecursos table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idRecurso"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Usuarios objects.</returns>
		public TList<Usuarios> GetByIdRecursoFromUsuariosRecursos(TransactionManager transactionManager, int _idRecurso)
		{
			int count = -1;
			return GetByIdRecursoFromUsuariosRecursos(transactionManager, _idRecurso, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets Usuarios objects from the datasource by IdRecurso in the
		///		UsuariosRecursos table. Table Usuarios is related to table Recursos
		///		through the (M:N) relationship defined in the UsuariosRecursos table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idRecurso"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Usuarios objects.</returns>
		public TList<Usuarios> GetByIdRecursoFromUsuariosRecursos(TransactionManager transactionManager, int _idRecurso,int start, int pageLength)
		{
			int count = -1;
			return GetByIdRecursoFromUsuariosRecursos(transactionManager, _idRecurso, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Usuarios objects from the datasource by IdRecurso in the
		///		UsuariosRecursos table. Table Usuarios is related to table Recursos
		///		through the (M:N) relationship defined in the UsuariosRecursos table.
		/// </summary>
		/// <param name="_idRecurso"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Usuarios objects.</returns>
		public TList<Usuarios> GetByIdRecursoFromUsuariosRecursos(int _idRecurso,int start, int pageLength, out int count)
		{
			
			return GetByIdRecursoFromUsuariosRecursos(null, _idRecurso, start, pageLength, out count);
		}


		/// <summary>
		///		Gets Usuarios objects from the datasource by IdRecurso in the
		///		UsuariosRecursos table. Table Usuarios is related to table Recursos
		///		through the (M:N) relationship defined in the UsuariosRecursos table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_idRecurso"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Usuarios objects.</returns>
		public abstract TList<Usuarios> GetByIdRecursoFromUsuariosRecursos(TransactionManager transactionManager,int _idRecurso, int start, int pageLength, out int count);
		
		#endregion GetByIdRecursoFromUsuariosRecursos
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, bookMaster.AssetSystem.Entities.UsuariosKey key)
		{
			return Delete(transactionManager, key.IdUsuario);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_idUsuario">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(string _idUsuario)
		{
			return Delete(null, _idUsuario);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idUsuario">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, string _idUsuario);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override bookMaster.AssetSystem.Entities.Usuarios Get(TransactionManager transactionManager, bookMaster.AssetSystem.Entities.UsuariosKey key, int start, int pageLength)
		{
			return GetByIdUsuario(transactionManager, key.IdUsuario, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_Usuarios index.
		/// </summary>
		/// <param name="_idUsuario"></param>
		/// <returns>Returns an instance of the <see cref="bookMaster.AssetSystem.Entities.Usuarios"/> class.</returns>
		public bookMaster.AssetSystem.Entities.Usuarios GetByIdUsuario(string _idUsuario)
		{
			int count = -1;
			return GetByIdUsuario(null,_idUsuario, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Usuarios index.
		/// </summary>
		/// <param name="_idUsuario"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="bookMaster.AssetSystem.Entities.Usuarios"/> class.</returns>
		public bookMaster.AssetSystem.Entities.Usuarios GetByIdUsuario(string _idUsuario, int start, int pageLength)
		{
			int count = -1;
			return GetByIdUsuario(null, _idUsuario, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Usuarios index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idUsuario"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="bookMaster.AssetSystem.Entities.Usuarios"/> class.</returns>
		public bookMaster.AssetSystem.Entities.Usuarios GetByIdUsuario(TransactionManager transactionManager, string _idUsuario)
		{
			int count = -1;
			return GetByIdUsuario(transactionManager, _idUsuario, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Usuarios index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idUsuario"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="bookMaster.AssetSystem.Entities.Usuarios"/> class.</returns>
		public bookMaster.AssetSystem.Entities.Usuarios GetByIdUsuario(TransactionManager transactionManager, string _idUsuario, int start, int pageLength)
		{
			int count = -1;
			return GetByIdUsuario(transactionManager, _idUsuario, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Usuarios index.
		/// </summary>
		/// <param name="_idUsuario"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="bookMaster.AssetSystem.Entities.Usuarios"/> class.</returns>
		public bookMaster.AssetSystem.Entities.Usuarios GetByIdUsuario(string _idUsuario, int start, int pageLength, out int count)
		{
			return GetByIdUsuario(null, _idUsuario, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Usuarios index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idUsuario"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="bookMaster.AssetSystem.Entities.Usuarios"/> class.</returns>
		public abstract bookMaster.AssetSystem.Entities.Usuarios GetByIdUsuario(TransactionManager transactionManager, string _idUsuario, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#region _Usuarios_ValidarUsuario 
		
		/// <summary>
		///	This method wrap the '_Usuarios_ValidarUsuario' stored procedure. 
		/// </summary>
		/// <param name="userName"> A <c>System.String</c> instance.</param>
		/// <param name="fingerprint"> A <c>System.String</c> instance.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="TList&lt;Usuarios&gt;"/> instance.</returns>
		public TList<Usuarios> ValidarUsuario(System.String userName, System.String fingerprint)
		{
			return ValidarUsuario(null, 0, int.MaxValue , userName, fingerprint);
		}
		
		/// <summary>
		///	This method wrap the '_Usuarios_ValidarUsuario' stored procedure. 
		/// </summary>
		/// <param name="userName"> A <c>System.String</c> instance.</param>
		/// <param name="fingerprint"> A <c>System.String</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="TList&lt;Usuarios&gt;"/> instance.</returns>
		public TList<Usuarios> ValidarUsuario(int start, int pageLength, System.String userName, System.String fingerprint)
		{
			return ValidarUsuario(null, start, pageLength , userName, fingerprint);
		}
				
		/// <summary>
		///	This method wrap the '_Usuarios_ValidarUsuario' stored procedure. 
		/// </summary>
		/// <param name="userName"> A <c>System.String</c> instance.</param>
		/// <param name="fingerprint"> A <c>System.String</c> instance.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="TList&lt;Usuarios&gt;"/> instance.</returns>
		public TList<Usuarios> ValidarUsuario(TransactionManager transactionManager, System.String userName, System.String fingerprint)
		{
			return ValidarUsuario(transactionManager, 0, int.MaxValue , userName, fingerprint);
		}
		
		/// <summary>
		///	This method wrap the '_Usuarios_ValidarUsuario' stored procedure. 
		/// </summary>
		/// <param name="userName"> A <c>System.String</c> instance.</param>
		/// <param name="fingerprint"> A <c>System.String</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="TList&lt;Usuarios&gt;"/> instance.</returns>
		public abstract TList<Usuarios> ValidarUsuario(TransactionManager transactionManager, int start, int pageLength , System.String userName, System.String fingerprint);
		
		#endregion
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;Usuarios&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;Usuarios&gt;"/></returns>
		public static TList<Usuarios> Fill(IDataReader reader, TList<Usuarios> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				bookMaster.AssetSystem.Entities.Usuarios c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("Usuarios")
					.Append("|").Append((string)reader[((int)UsuariosColumn.IdUsuario - 1)]).ToString();
					c = EntityManager.LocateOrCreate<Usuarios>(
					key.ToString(), // EntityTrackingKey
					"Usuarios",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new bookMaster.AssetSystem.Entities.Usuarios();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.IdUsuario = (string)reader[((int)UsuariosColumn.IdUsuario - 1)];
					c.OriginalIdUsuario = c.IdUsuario;
					c.NombreUsuario = (reader.IsDBNull(((int)UsuariosColumn.NombreUsuario - 1)))?null:(string)reader[((int)UsuariosColumn.NombreUsuario - 1)];
					c.UsuarioWindows = (short)reader[((int)UsuariosColumn.UsuarioWindows - 1)];
					c.Fingerprint = (string)reader[((int)UsuariosColumn.Fingerprint - 1)];
					c.Supervisor = (reader.IsDBNull(((int)UsuariosColumn.Supervisor - 1)))?null:(System.Int16?)reader[((int)UsuariosColumn.Supervisor - 1)];
					c.ClaveSupervisor = (reader.IsDBNull(((int)UsuariosColumn.ClaveSupervisor - 1)))?null:(string)reader[((int)UsuariosColumn.ClaveSupervisor - 1)];
					c.Habilitar = (short)reader[((int)UsuariosColumn.Habilitar - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="bookMaster.AssetSystem.Entities.Usuarios"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="bookMaster.AssetSystem.Entities.Usuarios"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, bookMaster.AssetSystem.Entities.Usuarios entity)
		{
			if (!reader.Read()) return;
			
			entity.IdUsuario = (string)reader[((int)UsuariosColumn.IdUsuario - 1)];
			entity.OriginalIdUsuario = (string)reader["IdUsuario"];
			entity.NombreUsuario = (reader.IsDBNull(((int)UsuariosColumn.NombreUsuario - 1)))?null:(string)reader[((int)UsuariosColumn.NombreUsuario - 1)];
			entity.UsuarioWindows = (short)reader[((int)UsuariosColumn.UsuarioWindows - 1)];
			entity.Fingerprint = (string)reader[((int)UsuariosColumn.Fingerprint - 1)];
			entity.Supervisor = (reader.IsDBNull(((int)UsuariosColumn.Supervisor - 1)))?null:(System.Int16?)reader[((int)UsuariosColumn.Supervisor - 1)];
			entity.ClaveSupervisor = (reader.IsDBNull(((int)UsuariosColumn.ClaveSupervisor - 1)))?null:(string)reader[((int)UsuariosColumn.ClaveSupervisor - 1)];
			entity.Habilitar = (short)reader[((int)UsuariosColumn.Habilitar - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="bookMaster.AssetSystem.Entities.Usuarios"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="bookMaster.AssetSystem.Entities.Usuarios"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, bookMaster.AssetSystem.Entities.Usuarios entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.IdUsuario = (string)dataRow["IdUsuario"];
			entity.OriginalIdUsuario = (string)dataRow["IdUsuario"];
			entity.NombreUsuario = Convert.IsDBNull(dataRow["NombreUsuario"]) ? null : (string)dataRow["NombreUsuario"];
			entity.UsuarioWindows = (short)dataRow["UsuarioWindows"];
			entity.Fingerprint = (string)dataRow["Fingerprint"];
			entity.Supervisor = Convert.IsDBNull(dataRow["Supervisor"]) ? null : (System.Int16?)dataRow["Supervisor"];
			entity.ClaveSupervisor = Convert.IsDBNull(dataRow["ClaveSupervisor"]) ? null : (string)dataRow["ClaveSupervisor"];
			entity.Habilitar = (short)dataRow["Habilitar"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="bookMaster.AssetSystem.Entities.Usuarios"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">bookMaster.AssetSystem.Entities.Usuarios Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, bookMaster.AssetSystem.Entities.Usuarios entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByIdUsuario methods when available
			
			#region IdRecursoRecursosCollection_From_UsuariosRecursos
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<Recursos>|IdRecursoRecursosCollection_From_UsuariosRecursos", deepLoadType, innerList))
			{
				entity.IdRecursoRecursosCollection_From_UsuariosRecursos = DataRepository.RecursosProvider.GetByIdUsuarioFromUsuariosRecursos(transactionManager, entity.IdUsuario);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'IdRecursoRecursosCollection_From_UsuariosRecursos' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.IdRecursoRecursosCollection_From_UsuariosRecursos != null)
				{
					deepHandles.Add("IdRecursoRecursosCollection_From_UsuariosRecursos",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< Recursos >) DataRepository.RecursosProvider.DeepLoad,
						new object[] { transactionManager, entity.IdRecursoRecursosCollection_From_UsuariosRecursos, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region UsuariosRecursosCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<UsuariosRecursos>|UsuariosRecursosCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'UsuariosRecursosCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.UsuariosRecursosCollection = DataRepository.UsuariosRecursosProvider.GetByIdUsuario(transactionManager, entity.IdUsuario);

				if (deep && entity.UsuariosRecursosCollection.Count > 0)
				{
					deepHandles.Add("UsuariosRecursosCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<UsuariosRecursos>) DataRepository.UsuariosRecursosProvider.DeepLoad,
						new object[] { transactionManager, entity.UsuariosRecursosCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the bookMaster.AssetSystem.Entities.Usuarios object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">bookMaster.AssetSystem.Entities.Usuarios instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">bookMaster.AssetSystem.Entities.Usuarios Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, bookMaster.AssetSystem.Entities.Usuarios entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region IdRecursoRecursosCollection_From_UsuariosRecursos>
			if (CanDeepSave(entity.IdRecursoRecursosCollection_From_UsuariosRecursos, "List<Recursos>|IdRecursoRecursosCollection_From_UsuariosRecursos", deepSaveType, innerList))
			{
				if (entity.IdRecursoRecursosCollection_From_UsuariosRecursos.Count > 0 || entity.IdRecursoRecursosCollection_From_UsuariosRecursos.DeletedItems.Count > 0)
				{
					DataRepository.RecursosProvider.Save(transactionManager, entity.IdRecursoRecursosCollection_From_UsuariosRecursos); 
					deepHandles.Add("IdRecursoRecursosCollection_From_UsuariosRecursos",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<Recursos>) DataRepository.RecursosProvider.DeepSave,
						new object[] { transactionManager, entity.IdRecursoRecursosCollection_From_UsuariosRecursos, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<UsuariosRecursos>
				if (CanDeepSave(entity.UsuariosRecursosCollection, "List<UsuariosRecursos>|UsuariosRecursosCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(UsuariosRecursos child in entity.UsuariosRecursosCollection)
					{
						if(child.IdUsuarioSource != null)
						{
								child.IdUsuario = child.IdUsuarioSource.IdUsuario;
						}

						if(child.IdRecursoSource != null)
						{
								child.IdRecurso = child.IdRecursoSource.IdRecurso;
						}

					}

					if (entity.UsuariosRecursosCollection.Count > 0 || entity.UsuariosRecursosCollection.DeletedItems.Count > 0)
					{
						//DataRepository.UsuariosRecursosProvider.Save(transactionManager, entity.UsuariosRecursosCollection);
						
						deepHandles.Add("UsuariosRecursosCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< UsuariosRecursos >) DataRepository.UsuariosRecursosProvider.DeepSave,
							new object[] { transactionManager, entity.UsuariosRecursosCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region UsuariosChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>bookMaster.AssetSystem.Entities.Usuarios</c>
	///</summary>
	public enum UsuariosChildEntityTypes
	{

		///<summary>
		/// Collection of <c>Usuarios</c> as ManyToMany for RecursosCollection_From_UsuariosRecursos
		///</summary>
		[ChildEntityType(typeof(TList<Recursos>))]
		IdRecursoRecursosCollection_From_UsuariosRecursos,

		///<summary>
		/// Collection of <c>Usuarios</c> as OneToMany for UsuariosRecursosCollection
		///</summary>
		[ChildEntityType(typeof(TList<UsuariosRecursos>))]
		UsuariosRecursosCollection,
	}
	
	#endregion UsuariosChildEntityTypes
	
	#region UsuariosFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;UsuariosColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Usuarios"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class UsuariosFilterBuilder : SqlFilterBuilder<UsuariosColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the UsuariosFilterBuilder class.
		/// </summary>
		public UsuariosFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the UsuariosFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public UsuariosFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the UsuariosFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public UsuariosFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion UsuariosFilterBuilder
	
	#region UsuariosParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;UsuariosColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Usuarios"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class UsuariosParameterBuilder : ParameterizedSqlFilterBuilder<UsuariosColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the UsuariosParameterBuilder class.
		/// </summary>
		public UsuariosParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the UsuariosParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public UsuariosParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the UsuariosParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public UsuariosParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion UsuariosParameterBuilder
	
	#region UsuariosSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;UsuariosColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Usuarios"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class UsuariosSortBuilder : SqlSortBuilder<UsuariosColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the UsuariosSqlSortBuilder class.
		/// </summary>
		public UsuariosSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion UsuariosSortBuilder
	
} // end namespace
